<?php

namespace Trois\Lib;

/**
 * 文件类型缓存类
 * 移植tp
 */
class File
{

    /**
     * 缓存连接参数
     * @access protected
     */
    protected array $options = [
        'temp' => EASYSWOOLE_ROOT . "/Temp/Cache/"
    ];

    /**
     * 架构函数
     * @access public
     */
    public function __construct($options = [])
    {
        if (!empty($options)) {
            $this->options = $options;
        }
        !empty($options['temp']) && $this->options['temp'] = $options['temp'];

        if (substr($this->options['temp'], -1) != '/') $this->options['temp'] .= '/';

        $this->init();
    }

    /**
     * 初始化检查
     * @access private
     * @return boolean
     */
    private function init(): void
    {
        // 创建应用缓存目录
        if (!is_dir($this->options['temp'])) {
            mkdir($this->options['temp']);
        }
    }

    /**
     * 取得变量的存储文件名
     * @access private
     * @param string $name 缓存变量名
     * @return string
     */
    private function filename(string $name): string
    {
        $name = md5($name);
        $filename = $name . '.php';
        return $this->options['temp'] . $filename;
    }

    /**
     * 读取缓存
     * @access public
     * @param string $name 缓存变量名
     * @return mixed
     */
    public function get(string $name)
    {
        $filename = $this->filename($name);
        if (!is_file($filename)) {
            return false;
        }

        $content = file_get_contents($filename);
        if (false !== $content) {
            $content = substr($content, 8, -3);
            $content = unserialize($content);
            return $content;
        } else {
            return false;
        }
    }

    /**
     * 写入缓存
     * @access public
     * @param string $name 缓存变量名
     * @param mixed $value 存储数据
     * @param int $expire 有效时间 0为永久
     * @return boolean
     */
    public function set(string $name, $value): bool
    {
        $filename = $this->filename($name);
        $data = serialize($value);

        $data = "<?php\n//" . $data . "\n?>";
        $result = file_put_contents($filename, $data);
        if ($result) {
            clearstatcache();
            return true;
        } else {
            return false;
        }
    }


    /**
     * 删除缓存
     * @access public
     * @param string $name 缓存变量名
     */
    public function rm(string $name): bool
    {
        return unlink($this->filename($name));
    }

    /**
     * 清除缓存
     * @access public
     * @param string $name 缓存变量名
     * @return boolean
     */
    public function clear(): bool
    {
        $path = $this->options['temp'];
        $files = scandir($path);
        if ($files) {
            foreach ($files as $file) {
                if ($file != '.' && $file != '..' && is_dir($path . $file)) {
                    array_map('unlink', glob($path . $file . '/*.*'));
                } elseif (is_file($path . $file)) {
                    unlink($path . $file);
                }
            }
            return true;
        }
        return false;
    }
}